from jinja2 import Environment,FileSystemLoader 
import argparse
import os
import shutil

def gen_sysconfig(serverID, cookie, serverName, saveDir, mysqlPort, mongoPort):
    if not os.path.exists(saveDir):
        os.makedirs(saveDir)

    env = Environment(loader=FileSystemLoader('sysconfig-template/'))
    
    env_template = env.get_template('env.template')
    env_config = env_template.render(server_id=serverID, mysql_port=mysqlPort, mongo_port=mongoPort)
    with open(os.path.join(saveDir, "sys.env"), "w") as f:
        f.write(env_config)

    docker_compose_template = env.get_template('docker-compose.yaml.template')
    docker_compose = docker_compose_template.render()
    with open(os.path.join(saveDir, "docker-compose.yaml"), "w") as f:
        f.write(docker_compose)

    start_server_template = env.get_template('start_server.sh.template')
    start_server = start_server_template.render()
    with open(os.path.join(saveDir, "start_server.sh"), "w") as f:
        f.write(start_server)

    stop_server_template = env.get_template('stop_server.sh.template')
    stop_server = stop_server_template.render()
    with open(os.path.join(saveDir, "stop_server.sh"), "w") as f:
        f.write(stop_server)

    start_server_ps_template = env.get_template('start_server.ps1.template')
    start_server_ps= start_server_ps_template.render()
    with open(os.path.join(saveDir, "start_server.ps1"), "w") as f:
        f.write(start_server_ps)

    stop_server_ps_template = env.get_template('stop_server.ps1.template')
    stop_server_ps = stop_server_ps_template.render()
    with open(os.path.join(saveDir, "stop_server.ps1"), "w") as f:
        f.write(stop_server_ps)


    configDir = os.path.join(saveDir, "gsconfig")
    if not os.path.exists(configDir):
        os.makedirs(configDir)
    rebar_template = env.get_template('rebar.config.template')
    rebar_config = rebar_template.render(server_id=serverID, cookie=cookie)
    with open(os.path.join(configDir, "rebar.config"), "w") as f:
        f.write(rebar_config)

    sysConfigDir = os.path.join(configDir, "sysconfig")
    if not os.path.exists(sysConfigDir):
        os.makedirs(sysConfigDir)
    vm_template = env.get_template('vm.args.template')
    vm_args = vm_template.render(server_id=serverID, cookie=cookie)
    with open(os.path.join(sysConfigDir, "vm.args"), "w") as f:
        f.write(vm_args)

    sys_template = env.get_template('sys.config.template')
    sys_config = sys_template.render(server_id=serverID, server_name=serverName, mysql_port=mysqlPort, mongo_port=mongoPort)
    with open(os.path.join(sysConfigDir, "sys.config"), "w") as f:
        f.write(sys_config)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--server_id", type=int, required=True)
    parser.add_argument("--cookie", type=str, required=True)
    parser.add_argument("--server_name", type=str, default="")
    parser.add_argument("--mongo_port", type=int, default=27017)
    parser.add_argument("--mysql_port", type=int, default=3306)
    parser.add_argument("--save", type=str, default="")

    args = parser.parse_args()
    serverName = args.server_name;
    if serverName == "":
        serverName = "game{}".format(args.server_id)
    save = args.save
    if save == "":
        save = "./{}-config".format(args.server_id)
    gen_sysconfig(args.server_id, args.cookie, serverName, save, args.mysql_port, args.mongo_port)
